package com.aidex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestApplication {

    public static void main(String[] args) {
        getLocation();
    }

    public static void execCommandAndGetOutput() {
        try {
            Runtime runtime = Runtime.getRuntime();
            String[] cmd = {"/bin/sh", "-c", " cli publish --platform mp-weixin --project examxcx --upload true --appid wxaf0dbd0b2afb0be0 --description 测试11 --version 1.01 --privatekey /Users/sean/Downloads/private.wxaf0dbd0b2afb0be0.key"};
            Process process = runtime.exec(cmd);
            // 输出结果，必须写在 waitFor 之前
            String outStr = getStreamStr(process.getInputStream());
            // 错误结果，必须写在 waitFor 之前
            String errStr = getStreamStr(process.getErrorStream());
            int exitValue = process.waitFor(); // 退出值 0 为正常，其他为异常
            System.out.println("exitValue: " + exitValue);

            process.destroy();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

    public static String getStreamStr(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));

        String line;
        while ((line = br.readLine()) != null) {
            StringBuilder sb = new StringBuilder();
            sb.append(line);
            System.out.println("outStr: " + sb);
        }
        br.close();
        return null;
    }

    public static void getLocation() {
        String string = "1234567TIME9000";
        System.out.println(string.substring(3, string.indexOf("TIME")));
    }
}
